package tech.pdai.springframework.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class WindowsCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        System.out.println("WindowsCondition.class");
        // 获取到类加载器
        ClassLoader classLoader = context.getClassLoader();
        System.out.println(classLoader.getClass());
        // 获取到bean工厂
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        // 获取到bean定义的注册类
        BeanDefinitionRegistry registry = context.getRegistry();
        System.out.println(registry.getClass());
        String[] beanDefinitionNames = registry.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
        // 获取到环境信息
        Environment environment = context.getEnvironment();
        // 获取到资源加载器
        ResourceLoader resourceLoader = context.getResourceLoader();


        System.out.println("WindowsCondition.class");
        // 获取操作系统
        String property = environment.getProperty("os.name");
        return property != null && property.toLowerCase().contains("windows");
    }
}
